$(function () {
    var form = layui.form
    getInfo()

    form.verify({
        nikname: function (val) {
            if (val.length > 6) {
                return '用户名不得超过6位哦'
            }
        }
    })

    $('.layui-form').on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "/my/userinfo",
            data: $(this).serialize(),
            success: function (res) {
                console.log(res);
                layer.msg('更新用户信息成功')
                window.parent.getInfo();
            }
        });
    })

    $('#reset').on('click', function (e) {
        e.preventDefault();
        getInfo();
    })
})

function getInfo() {
    $.ajax({
        type: "GET",
        url: "/my/userinfo",
        success: function (res) {
            if (res.status !== 0) {
                layer.msg('获取用户信息失败')
            }
            console.log(res)
            showInfo(res.data)
        }
    });
}

function showInfo (user) {
    let form = layui.form
    // $('#loginName').val(user.username);
    // $('#nickname').val(user.nickname)
    // $('#useremail').val(user.email)
    // $('#userid').val(user.id)
    form.val('user_info', user)
}



